package com.example.jasonrpc.example;

import com.example.jasonrpc.registry.RegistryCenter;
import com.example.jasonrpc.registry.redis.RedisRegistry;
import com.example.jasonrpc.server.RpcServerAbstract;
import com.example.jasonrpc.server.byRegistry.RpcServerByRegistry;
import com.example.jasonrpc.server.byRegistry.RpcServerConfigByRegistry;
import com.example.jasonrpc.server.simple.RpcServer;

import java.rmi.RemoteException;

public class Server {

    public static void main(String[] args) throws RemoteException {
        String serverIp = ConfigUtil.getServerIp("config");
        int serverPort = ConfigUtil.getServerPort("config");
        int centerPort = ConfigUtil.getCenterPort("config");
        //rpc注册服务和将其添加到注册中心应该是一个事务内，至少一个方法内
        //使用注册中心
//        startServerByRegistry(serverIp, serverPort, centerPort);
        //不使用注册中心
        RpcServer server = new RpcServer(serverPort);
        server.register(CalcService.class, new CalcServiceImpl());
        server.register(StringService.class, new StringServiceImpl());
        server.start();
    }

    private static void startServerByRegistry(String serverIp, int serverPort, int centerPort) throws RemoteException {
        RegistryCenter center = new RedisRegistry(serverIp, centerPort);
        RpcServerConfigByRegistry config = new RpcServerConfigByRegistry(center, serverPort);
        RpcServerAbstract server = new RpcServerByRegistry(config);
        //rpc服务端与服务提供方在同一主机
        server.register(CalcService.class, new CalcServiceImpl());
        server.register(StringService.class, new StringServiceImpl());
        server.start();
    }
}
